<template>
  <div style="margin: 10px 0">
    <el-table :data="tableData" stripe style="border-radius: 10px">
      <el-table-column label="环境照片1"><template slot-scope="scope"><el-image style="width: 100px; height: 100px" :src="$store.state.baseApi+scope.row.img" :preview-src-list="[$store.state.baseApi + scope.row.img]"></el-image></template></el-table-column>
      <el-table-column label="环境照片2"><template slot-scope="scope"><el-image style="width: 100px; height: 100px" :src="$store.state.baseApi+scope.row.img2" :preview-src-list="[$store.state.baseApi + scope.row.img2]"></el-image></template></el-table-column>
      <el-table-column label="环境照片3"><template slot-scope="scope"><el-image style="width: 100px; height: 100px" :src="$store.state.baseApi+scope.row.img3" :preview-src-list="[$store.state.baseApi + scope.row.img3]"></el-image></template></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column prop="information" label="相关描述"></el-table-column>

    </el-table>
    <div style="padding: 10px; margin: 10px 0; background-color: #fff; border-radius: 10px">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, prev, pager, next"
          :total="total">
      </el-pagination>
    </div>
    <br>
    <br>

    <el-dialog title="信息" :visible.sync="dialogFormVisible" width="30%" :close-on-click-modal="false">
      <el-form label-width="100px"  style="width: 90%">
        <el-form-item label="环境照片1">
          <el-upload :action="$store.state.baseApi+'/file/upload'" ref="img" :on-success="handleImgUploadSuccess">
            <el-button  type="primary"> 点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="环境照片2">
          <el-upload :action="$store.state.baseApi+'/file/upload'" ref="img" :on-success="handleImgUploadSuccess2">
            <el-button  type="primary"> 点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="环境照片3">
          <el-upload :action="$store.state.baseApi+'/file/upload'" ref="img" :on-success="handleImgUploadSuccess3">
            <el-button  type="primary"> 点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="相关描述">
          <el-input v-model="form.information" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">  取 消</el-button>
        <el-button type="primary" @click="save"> 确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Feed",
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      name: "",
      form: {},
      dialogFormVisible: false,
      multipleSelection: [],
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      this.request.get("/feed/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name,
        }
      }).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    save() {
      this.request.post("/feed", this.form).then(res => {
        if (res.code === '200') {
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
      this.$nextTick(() => {
        if(this.$refs.img) {
           this.$refs.img.clearFiles();
         }
        if(this.$refs.img3) {
          this.$refs.img3.clearFiles();
        }
        if(this.$refs.img2) {
          this.$refs.img2.clearFiles();
        }
      })
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogFormVisible = true
       this.$nextTick(() => {
         if(this.$refs.img) {
           this.$refs.img.clearFiles();
         }
         if(this.$refs.img3) {
          this.$refs.img3.clearFiles();
         }
         if(this.$refs.img2) {
           this.$refs.img2.clearFiles();
         }
       })
    },
    del(id) {
      this.request.delete("/feed/" + id).then(res => {
        if (res.code === '200') {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      if (!this.multipleSelection.length) {
        this.$message.error("请选择需要删除的数据")
        return
      }
      let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
      this.request.post("/feed/del/batch", ids).then(res => {
        if (res.code === '200') {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    reset() {
      this.name = ""
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },
    handleFileUploadSuccess(res) {
      this.form.file = res
    },
    handleImgUploadSuccess(res) {
      this.form.img = res
    },
    handleImgUploadSuccess2(res) {
      this.form.img2 = res
    },
    handleImgUploadSuccess3(res) {
      this.form.img3 = res
    },
    download(url) {
      window.open(url)
    },
    exp() {
      window.open(this.$store.state.baseApi+"/feed/export")
    },
    handleExcelImportSuccess() {
      this.$message.success("导入成功")
      this.load()
    }
  }
}
</script>


<style>
.headerBg {
  background: #eee!important;
}
</style>
